<?php
	class userClass
	{
		public $uid, $userName, $userInfo;
		protected $userPwd, $userStatus, $conn;
		
		function __construct($uid = '')
		{
			$this->setup();
			if ($uid != '') 
			{
				$this->uid = $uid;
				$this->refreshInfo();
			}
		}
		
		private function setup()
		{
			$this->conn = sqlConnect();
			$this->conn->exec("set names utf8;");
		}
		
		private function refreshInfo()
		{
			$result = $this->conn->query("SELECT * FROM user WHERE uid={$this->uid};");
			$result->setFetchMode(PDO::FETCH_ASSOC);
			$info = $result->fetch();
			
			$this->userName = $info['userName'];
			$this->userInfo = $info['userInfo'];
			$this->userPwd = $info['userPwd'];
			$this->userStatus = $info['userStatus'];
		}
		
		protected function registry($userName, $userPwd)
		{
			if ($this->checkNameValidity($userName))
			{
				$this->uid = $this->getNewUid();
				$this->userName = $userName;
				$this->userPwd = md5($userPwd);
				$this->userInfo = '';
				
				if ($this->create())
				{
					return $this->updateRecord();
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		public static function doReg($info)
		{
			if ($info['userPwd'] != $info['userRePwd'])
			{
				return false;
			}
			else 
			{
				$user = new userClass();
				return $user->registry($info['userName'], $info['userPwd']);
			}
		}
		
		protected function login($userName, $userPwd)
		{
			$result = $this->conn->query("SELECT userPwd, uid FROM user WHERE userName='{$userName}';");
			$result->setFetchMode(PDO::FETCH_ASSOC);
			$info = $result->fetch();
			if ($info[userPwd] == md5($userPwd))
			{
				return $info[uid];
			}
			else 
			{
				return false;
			}
		}
		
		public static function doLogin($info)
		{
			$person = new userClass();
			$uid = $person->login($info['userName'], $info['userPwd']);
			if ($uid)
			{
				$_SESSION['userName'] = $info['userName'];
				$_SESSION['uid'] = $uid;
				$_SESSION['authPassed'] = 'True';
			}
		}
		
		private function checkNameValidity($userName)
		{
			$result = $this->conn->query("SELECT uid FROM user WHERE userName='{$userName}';");
			return !$result->fetchColumn();
		}
		
		protected function getNewUid()
		{
			$result = $this->conn->query("SELECT value FROM record WHERE name='userAmount'");
			$result->setFetchMode(PDO::FETCH_ASSOC);
			$info = $result->fetch();
			return $info['value'] + 1;
		}
		
		protected function create()
		{
			return $this->conn->exec("INSERT INTO user SET uid={$this->uid}, userName='{$this->userName}', userPwd='{$this->userPwd}', userInfo='{$this->userInfo}';");
		}
		
		protected function updateRecord()
		{
			return $this->conn->exec("UPDATE record SET value='{$this->uid}' WHERE name='userAmount';");
		}
		
		public function setupInfo()
		{
			$separateArray = explode(';', $this->userInfo);
			foreach ($separateArray as $row)
			{
				$rowInfo = explode(':', $row);
				$result[$rowInfo[0]] = $rowInfo[1];
			}
			return $result;
		}
		
		public function updateInfo($infoArray)
		{
			$infoString = '';
			
			foreach ($infoArray as $key=>$value)
			{
				$infoString = $infoString.$key.':'.$value.';';
			}
			$this->userInfo = substr($infoString, 0, strripos($infoString, ';') - 1);
			$this->update();
		}
		
		public function update()
		{
			return $this->conn->exec("UPDATE user SET userInfo='{$this->userInfo}', userPwd='{$this->userPwd}' WHERE uid={$this->uid};");
		}
		
		public function uploadFile($file, $type='file', $album='')
		{
			$processUpload = new uploadClass(UPLOADPATH."{$this->uid}/{$type}/");
			if ($processUpload->upload($file))
			{
				return $this->updateFileInfo($file['name'], $file['size'], $album);
			}
		}
		
		private function updateFileInfo($fileName, $fileSize, $album)
		{
			return $this->conn->exec("INSERT INTO file SET uid={$this->uid}, userName='{$this->userName}', fileName='{$fileName}', fileSize='{$fileSize}', albumName='{$album}';");
		}
		
		public function showFileInfo($fileName='', $albumName='')
		{
			if ($fileName != '')
			{
				$result = $this->conn->query("SELECT * FROM file WHERE uid={$this->uid} AND fileName='{$fileName}';");
			}
			else if ($albumName != '')
			{
				$result = $this->conn->query("SELECT * FROM file WHERE uid={$this->uid} AND albumName='{$albumName}';");
			}
			else 
			{
				$result = $this->conn->query("SELECT * FROM file WHERE uid={$this->uid} AND albumName='';");
			}
			$result->setFetchMode(PDO::FETCH_ASSOC);
			return $result->fetchAll();
		}
		
		function __destruct()
		{
			$this->conn = null;
		}
	}
	
	class teamClass extends userClass
	{
		//the userPwd will be teamLeader in this class
		public function registry($teamName, $teamLeader, $teamCompetition)
		{
			if ($this->checkNameValidity($teamName, $teamCompetition))
			{
				$this->uid = $this->getNewUid();
				$this->userName = $teamName;
				$this->userPwd = $teamLeader;
				$this->userInfo = "teamCompetition:{$teamCompetition};teamMember:{$this->Pwd};";
				
				if ($this->create())
				{
					return $this->updateRecord();
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		private function checkNameValidity($teamName, $teamCompetition)
		{
			$result = $this->conn->query("SELECT uid FROM user WHERE userName='{$teamName}' AND userInfo LIKE '%teamCompetition={$teamCompetition}%';");
			return !$result->fetchColumn();
		}
	
		public function addMember($memberUid)
		{
			$info = $this->setupInfo();
			
			//replace with preg_match
			if ($memberUid != $this->userPwd && !strpos($info[teamMember], ",{$memberUid}"))
			{
				$info[teamMember] = $info[teamMember].",{$memberUid}";
				$this->updateInfo($info);
			}
		}

		public function getMember()
		{
			$info = $this->setupInfo();
			return explode(',', $info[teamMember]);
		}

	}
	
	class competitionClass extends userClass
	{
		//the userPwd will be competitionAdmin in this class
		
		public function registry($competitionName, $competitionAdmin)
		{
			if ($this->checkNameValidity($competitionName))
			{
				$this->uid = $this->getNewUid();
				$this->userName = $competitionName;
				$this->userPwd = $competitionAdmin;
				$this->userInfo = "competitionAdmin:{$competitionAdmin};";
				
				if ($this->create())
				{
					return $this->updateRecord();
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		private function checkNameValidity($competitionName)
		{
			$result = $this->conn->query("SELECT uid FROM user WHERE userName='{$competitionName}' AND userInfo LIKE '%competition%';");
			return !$result->fetchColumn();
		}
		
		
	}
	
	